package com.hunktimes.wholesale.net

import android.text.TextUtils
import com.hunktimes.wholesale.BuildConfig
import com.hunktimes.wholesale.net.model.*
import com.hunktimes.wholesale.util.Constants
import com.hunktimes.wholesale.util.SP
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.Headers
import retrofit2.http.POST
import retrofit2.http.Query
import java.util.concurrent.TimeUnit

/**
 * Author: Delete
 * Date: 2020/7/22 17:20
 * Description:
 *
 */
interface IServer {
    companion object {
        fun get(): IServer {
            val okClientBuilder = OkHttpClient.Builder()
                .readTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .connectTimeout(20, TimeUnit.SECONDS)
            //.addInterceptor(JrHeaderInterceptor())
            if (BuildConfig.DEBUG) {
                val logInterceptor =
                    HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY }
                okClientBuilder.addInterceptor(logInterceptor)
            }
            return Retrofit.Builder()
                .baseUrl(Constants.base_url)
                .client(okClientBuilder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(IServer::class.java)
        }
    }

    class JrHeaderInterceptor : Interceptor {
        override fun intercept(chain: Interceptor.Chain): Response {
            val request = chain.request()
            val newBuilder = request.newBuilder()
            val headerValues = request.headers("token")
            newBuilder.removeHeader("token")
            if (null != headerValues && !headerValues.isEmpty()) {
                val headerValue = headerValues[0]
                //L.d("headerValue -> $headerValue")
                if (TextUtils.equals(headerValue, "no")) {
                    //不加 token
                    //L.d("Header 不加token")
                }
            } else {
                //L.d("Header 加 token")
                newBuilder.addHeader("token", SP.getToken())
            }
            return chain.proceed(newBuilder.build())
        }
    }

    @Headers("token:no")
    @POST("user/loginOrReg")
    suspend fun login(@Body loginOrReg: LoginOrReg): ApiResponse<String>

    @Headers("token:no")
    @POST("user/loginOrReg")
    suspend fun getSmsCode(@Body getCaptcha: GetCaptcha): ApiResponse<Any?>

    @Headers("token:no")
    @POST("user/loginOrReg")
    suspend fun forgetPwd(@Body forgetPwd: ForgetPwd): ApiResponse<Any?>

    @Headers("token:no")
    @POST("user/loginOrReg")
    suspend fun getBanners(): ApiResponse<List<String>>

    @Headers("token:no")
    @POST("user/loginOrReg")
    suspend fun getHomeData(): ApiResponse<List<String>>

    @Headers("token:no")
    @POST("user/loginOrReg")
    suspend fun getCategoryTags(): ApiResponse<List<String>>

    @Headers("token:no")
    @POST("user/loginOrReg")
    suspend fun queryCategoryByTag(@Query("tag") tag: String): ApiResponse<List<String>>

    @Headers("token:no")
    @POST("user/loginOrReg")
    suspend fun getCartGoods(): ApiResponse<List<String>>

    @Headers("token:no")
    @POST("user/loginOrReg")
    suspend fun getCartRecommends(): ApiResponse<List<String>>

    @Headers("token:no")
    @POST("user/loginOrReg")
    suspend fun getUserInfo(): ApiResponse<List<String>>

    @Headers("token:no")
    @POST("user/loginOrReg")
    suspend fun queryOrdersByType(@Query("type") tag: String): ApiResponse<List<String>>

    @Headers("token:no")
    @POST("user/loginOrReg")
    suspend fun getCollections(): ApiResponse<List<String>>


    @Headers("token:no")
    @POST("user/loginOrReg")
    suspend fun queryProdDetail(@Query("productId") productId: String): ApiResponse<List<String>>

    @POST("user/loginOrReg")
    suspend fun queryOrderDetail(@Query("orderId") orderId: String): ApiResponse<List<String>>

    /**
     * 请求 默认 收货地址
     */
    @POST("user/loginOrReg")
    suspend fun getDefRecAddr(): ApiResponse<String>


    /**
     * 请求 全部 收货地址
     */
    @POST("user/loginOrReg")
    suspend fun getRecAddrList(): ApiResponse<List<String>>

    /**
     * 更新 收货地址
     */
    @POST("user/loginOrReg")
    suspend fun updateAddress(recAddress: RecAddress): ApiResponse<String>


    /**
     * 更新 个人信息
     */
    @POST("user/loginOrReg")
    suspend fun updateMemberInfo(memberInfo: MemberInfo): ApiResponse<String>

}